package com.xxxy.hyc.xyxmp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.xxxy.hyc.xyxmp.R;
import com.xxxy.hyc.xyxmp.bean.Goods;
import com.xxxy.hyc.xyxmp.utils.CartManager;

public class GoodsDetailActivity extends AppCompatActivity {
    private ImageView ivGoodsDetail;
    private TextView tvNameDetail, tvPriceDetail, tvStockDetail, tvDescDetail, tvCount;
    private Button btnMinus, btnPlus, btnAddCart, btnGoToCart;
    private int count = 1;
    private Goods goods;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_detail);
        
        // 显示返回按钮
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }

        // 获取传递的商品数据
        Intent intent = getIntent();
        if (intent != null) {
            int goodsId = intent.getIntExtra("goods_id", 1);
            // 根据ID查找对应的商品
            goods = findGoodsById(goodsId);
        }

        // 初始化控件
        initViews();
        // 填充商品数据
        fillGoodsData();
        // 设置点击事件
        setClickEvents();
    }

    private Goods findGoodsById(int id) {
        // 模拟从数据源获取商品信息
        switch (id) {
            case 1:
                return new Goods(1, "可乐", 3.0, 100, "cola", "冰镇可乐330ml");
            case 2:
                return new Goods(2, "薯片", 5.0, 50, "chips", "原味薯片100g");
            case 3:
                return new Goods(3, "矿泉水", 2.0, 200, "water", "天然矿泉水550ml");
            case 4:
                return new Goods(4, "巧克力", 8.0, 30, "chocolate", "牛奶巧克力50g");
            default:
                return new Goods(1, "可乐", 3.0, 100, "cola", "冰镇可乐330ml");
        }
    }

    private void initViews() {
        ivGoodsDetail = findViewById(R.id.iv_goods_detail);
        tvNameDetail = findViewById(R.id.tv_name_detail);
        tvPriceDetail = findViewById(R.id.tv_price_detail);
        tvStockDetail = findViewById(R.id.tv_stock_detail);
        tvDescDetail = findViewById(R.id.tv_desc_detail);
        tvCount = findViewById(R.id.tv_count);
        btnMinus = findViewById(R.id.btn_minus);
        btnPlus = findViewById(R.id.btn_plus);
        btnAddCart = findViewById(R.id.btn_add_cart);
        btnGoToCart = findViewById(R.id.btn_go_to_cart); // 添加跳转到购物车按钮
    }

    private void fillGoodsData() {
        if (goods != null) {
            tvNameDetail.setText(goods.getName());
            tvPriceDetail.setText("¥" + goods.getPrice());
            tvStockDetail.setText("库存：" + goods.getStock());
            tvDescDetail.setText(goods.getDescription());

            // 加载商品图片（同商品列表逻辑）
            int resId = getResources().getIdentifier(
                    goods.getImageUrl(), "mipmap", getPackageName());
            if (resId == 0) resId = R.mipmap.ic_launcher;
            ivGoodsDetail.setImageResource(resId);
        }
    }

    private void setClickEvents() {
        // 减少数量
        btnMinus.setOnClickListener(v -> {
            if (count > 1) {
                count--;
                tvCount.setText(String.valueOf(count));
            }
        });

        // 增加数量
        btnPlus.setOnClickListener(v -> {
            if (count < goods.getStock()) { // 不能超过库存
                count++;
                tvCount.setText(String.valueOf(count));
            }
        });

        // 加入购物车
        btnAddCart.setOnClickListener(v -> {
            // 添加商品到购物车
            CartManager.getInstance().addToCart(goods, count);
            android.widget.Toast.makeText(this, "已加入购物车，数量：" + count, android.widget.Toast.LENGTH_SHORT).show();
        });
        
        // 跳转到购物车页面
        btnGoToCart.setOnClickListener(v -> {
            Intent cartIntent = new Intent(GoodsDetailActivity.this, CartActivity.class);
            startActivity(cartIntent);
        });
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}